export class DefenderTask implements ICreepTask{
  run(creep: Creep): void {
    let target:Creep | Structure<StructureConstant> | null = null;
    if (creep.hits < creep.hitsMax * 0.9 && creep.body.some(part => part.type === HEAL)) {
      creep.heal(creep);
    }
    if(creep.memory.attackTarget){
      console.log(creep.memory.attackTarget);
      target = Game.getObjectById(creep.memory.attackTarget);
      if(target){
        let attackResult = creep.attack(target);
        let rangedAttackResult = creep.rangedAttack(target);
        if(attackResult === ERR_NOT_IN_RANGE || rangedAttackResult === ERR_NOT_IN_RANGE){
          creep.moveTo(target);
        }
      }
      else {
        delete creep.memory.attackTarget;
      }
    }
    else {
      // 判断是不是外矿守护者
      if(creep.memory.outerRoom){
        const outerRoom = Game.rooms[creep.memory.outerRoom];
        if(outerRoom){
          // 如果不在指定房间内则走到指定房间内
          if(creep.pos.getRangeTo(new RoomPosition(25,25,outerRoom.name)) > 5){
            creep.go(new RoomPosition(25,25,outerRoom.name));
          }
          if(outerRoom.memory.enemys.length > 0){
            creep.memory.attackTarget = outerRoom.memory.enemys[0];
          }
        }
      }
    }
  }
}